<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/8
 * Time: 13:22
 */
namespace App\Http\Controllers\Api;

use App\Service\Api\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Constant\ErrorConstant;

class UserController extends BaseController
{
    protected $service;

    public function __construct(Request $request, UserService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 用户个人信息更新
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoUser()
    {
        $validator  =   Validator::make($this->request->post(), [
            'decrypt_data'  =>  'required|string',
            'decrypt_iv'    =>  'required|string',
        ], [
            'decrypt_data.required' =>  '待解析数据不能为空',
            'decrypt_iv.required'   =>  '待解析向量不能为空',
            'decrypt_data.string'   =>  '待解析数据必须为字符',
            'decrypt_iv.string'     =>  '待解析向量必须为字符'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_USER_INFO_ERROR, $validator->errors()->first());
        }

        $decrypt_data   =   $this->request->post('decrypt_data');
        $decrypt_iv     =   $this->request->post('decrypt_iv');
        [$flag, $result]    =   $this->service->userInfo($decrypt_data, $decrypt_iv);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_INFO_ERROR, $result);
        }
    }

    /**
     * 用户领取优惠券列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function couponUser()
    {
        $validator  =   Validator::make($this->request->post(), [
            'is_active' =>  'required|in:0,1',
        ], [
            'is_active.required'  =>   '是否有效不能为空',
            'is_active.in'        =>   '是否有效选择错误',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_USER_COUPON_ERROR, $validator->errors()->first());
        }

        $is_active  =   $this->request->post('is_active');

        [$flag, $result]    =   $this->service->userCoupon((int) $is_active);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_COUPON_ERROR, $result);
        }
    }

    /**
     * 用户中心数据统计
     * @return \Illuminate\Http\JsonResponse
     */
    public function centerUser()
    {
        [$flag, $result]    =   $this->service->userCenter();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_CENTER_ERROR, $result);
        }
    }

    /**
     * 用户上传图片
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadUser()
    {
        $validator = Validator::make($this->request->all(), [
            'file'  =>  'required|image|max:5120'
        ], [
            'file.required'=>  '上传图片不能为空',
            'file.image'   =>  '上传图片格式不正确',
            'file.max'     =>  '上传图片过大'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_USER_UPLOAD_ERROR, $validator->errors()->first());
        }

        $file   =   $this->request->all();
        [$flag, $result] = $this->service->userUpload($file['file']);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_UPLOAD_ERROR, $result);
        }
    }

    /**
     * 用户中心数据统计
     * @return \Illuminate\Http\JsonResponse
     */
    public function memberUser()
    {
        [$flag, $result]    =   $this->service->userMember();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_MEMBER_ERROR, $result);
        }
    }

    /**
     * 海报生成
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function posterUser()
    {
        [$flag, $result]    =   $this->service->userPoster();
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_POSTER_ERROR, $result);
        }
    }

    /**
     * 用户头像生成
     * @return \Illuminate\Http\JsonResponse
     */
    public function avatarUser()
    {
        $validator = Validator::make($this->request->all(), [
            'file'  =>  'required|image|max:5120'
        ], [
            'file.required'=>  '上传头像不能为空',
            'file.image'   =>  '上传头像格式不正确',
            'file.max'     =>  '上传头像过大'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_USER_AVATAR_ERROR, $validator->errors()->first());
        }

        $file   =   $this->request->all();
        [$flag, $result] = $this->service->userAvatar($file['file']);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_AVATAR_ERROR, $result);
        }
    }

    /**
     * 用户基本资料修改
     * @return \Illuminate\Http\JsonResponse
     */
    public function basicUser()
    {
        $validator = Validator::make($this->request->post(), [
            'nickname'  =>  'string|max:200',
            'phone'     =>  'string|regex:/^1[345789][0-9]{9}$/',
            'gender'    =>  'integer|in:0,1,2',
            'birthday'  =>  'string|date',
        ], [
            'nickname.string'   =>  '用户昵称必须为字符',
            'nickname.max'      =>  '用户昵称过长',
            'phone.string'      =>  '手机号必须为字符',
            'phone.regex'       =>  '手机号格式不正确',
            'gender.integer'    =>  '用户性别必须为整型',
            'gender.in'         =>  '用户性别选择错误',
            'birthday.string'   =>  '用户生日必须为字符',
            'birthday.date'     =>  '用户生日必须为日期'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_USER_BASIC_ERROR, $validator->errors()->first());
        }

        [$flag, $result] = $this->service->userBasic($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_USER_BASIC_ERROR, $result);
        }
    }

}
